<template>
  <div>
    <input type="number" v-model="Critical" min="0" max="100" @input="onInput" >
    <button @click="attack()">click</button>
  </div>
</template>
<script>
export default {
  data() {
    return {
      Critical: 0, //暴击率
      baseCritical: 0, //基础暴击率
      defCritical: 0, //临时暴击率
      delta: "", //增量
      n: 10, //次数
      i: 0,
    };
  },
  methods: {
    hit(x) {
      //生成随机数
      var rand = (function () {
        var today = new Date();
        var seed = today.getTime();
        function rnd() {
          seed = (seed * 9301 + 49297) % 233280;
          return seed / 233280.0;
        }
        return function rand(number) {
          // return Math.ceil(rnd(seed) * number);
          return Math.ceil(rnd() * number);
        };
      })();
      var myNum = rand(x);
      return myNum;
    },
     attack() {
      var that = this;
      if(!(that.Critical&&that.Critical>0)){
      that.Critical = 0
      alert('设置的暴击率不对')
        return
      }
      that.i++;
      // that.n = 12 - that.Critical / 10;
      that.baseCritical = that.Critical;
      var randNum = that.hit(100);
      that.delta = that.calc2(parseFloat(that.Critical));
      that.defCritical =
        parseFloat(that.baseCritical) + parseFloat(that.delta * (that.i - 1));
      // console.log((that.delta*(that.i-1)),that.baseCritical,that.i);'
      if (randNum > that.defCritical) {
        console.log(
          `第${that.i}次攻击，本次暴击率${that.defCritical},没有暴击`,
          randNum
        );
        alert('可恶，没有暴击。')
      } else {
        console.log(
          `第${that.i}次攻击，本次暴击率${that.defCritical},暴击了`,
          randNum
        );
        alert('暴击了！！！')
        that.reset();
      }
    },
    reset() {
      this.i = 0;
      this.defCritical = 0;
      this.baseCritical = this.Critical;
    },
    calc1(C) {
      var dCurP = 0.0;
      var dPreSuccessP = 0.0;
      var dPE = 0;
      var nMaxFail = Math.ceil(1.0 / C); //最大失败次数
      for (var i = 1; i <= nMaxFail; ++i) {
        dCurP = Math.min(1.0, i * C) * (1 - dPreSuccessP); //
        dPreSuccessP += dCurP;
        dPE += i * dCurP;
      }
      return 1.0 / dPE;
      console.log(1.0 / dPE);
    },
    calc2(P) {
      var dUp = P;
      var dLow = 0.0;
      var dMid = P;
      var dPLast = 1.0;
      while (true) {
        dMid = (dUp + dLow) / 2.0;
        var dPtested = this.calc1(dMid); // 使用二分法，不断试验当前C对应的实际概率
        if (Math.abs(dPtested - dPLast) <= 0.0)
          // 前后两次计算结果相同，说明到了逼近极限，不与P比较是因为有误差，可能永远无法再逼近
          break;
        if (dPtested > P)
         dUp = dMid;
        else dLow = dMid;
        dPLast = dPtested;
      }
      // console.log(dMid,'mid');
      return dMid;
    },
    calc() {
      {
        for (var i = 1; i <= 100; ++i) {
          var p = i / 100.0;
          var C = this.calc2(p);
          console.log( p, C);
          // printf(" PE=%lf  C=%lf\n ", p, C);
        }
        return 0;
      }
    },
    onInput() {
      this.reset();
    },
  },
  mounted() {
    // this.hit(100);
  },
};
</script>